#!/usr/bin/perl -w

#------------------------------------------------------
# This script is to track if clients open the emails,
# then write the tracking result into report.html; 
# If the recipients didn't open the email after three
# days, send alter emails to Medlytix manager.
#
#   Usage:
#       track_emails.pl
#
#   Author:
#       Tao Zhang   
#------------------------------------------------------ 


use strict;
use warnings;
use POSIX qw(:signal_h);
use Mail::IMAPClient; 
use Mail::IMAPClient::BodyStructure;
use Net::Domain  qw(hostname);
use MIME::Lite;
use Getopt::Std;
use MIME::Base64;
use HTTP::Date   qw(:DEFAULT parse_date);
use Time::Zone;
use Time::Local;
use Proc::Daemon;
use POSIX qw( strftime );
use Socket;
use DBI;

#------------------global variables---------------------------
my %file_record = ();         #key is the pair of CID & OID
my %file_record_score = ();         #key is the pair of CID & date
my $email_account_new  = 
    'email_pickup_alert@medlytix.com';      # email account
my $email_passwd = 'Pn!v9em';           # passwd 4 email account 
my $alertemail = 
    'tao.zhang@medlytix.com';       # account received alert
my $to_stop = 0;                # stop flag
my $DIR = '/home/t0612z/workspace/etrack_srv6';  # Path for daemon
my $REPORT = "/var/www/report.html";
my $score_REPORT = "/var/www/scoreport.html"; 
my $REPORT_File_Name = "report.html";
my $score_REPORT_File_Name = "scoreport.html";
my $DEBUG = 1;                  # flag for debug, 1 yes
my @ftp_cust_ids =( 15 , 18 , 19 , 23 , 24 , 39);   #payerintel custs who use ftp
my $screen_files_directory = "/var/www/record";
my $screen_payerintel_files_name = "overwrite_payerintel_records.txt";
my $screen_score_files_name = "overwrite_score_records.txt";
my $dashboard_message_backup = "db_mes_backup.txt"; #save the sending message
my %screen_payerintel_files_list = ();
my %screen_score_files_list = ();
my %payerintel_files_not_picked_up = ();
my $db="dashboard_db";     # database name
my $hostname='localhost';  # hostname
my $user='dashboard_u';    # user name to access DB
my $password='dashboard';  # passwd to access DB
my $software_version = "3.91";
my $today = undef;
my %org_hash = (                            # Big orgnization list
    "46" => "Air Methods",
    "4"  => "HSI Financial Services LLC",
    "18" => "American Health Services",
    "10" => "Hamilton Hospital",
    "12" => "Pitt Memorial",
    "25" => "Piedmont-Newnan Hospital",
    "16"=>   "HBCS",
    "3"=>   "HSI Financial Services LLC",
    "5"=>   "TeamHealth Bad Debt",
    "15"=>  "TeamHealth",
    "21"=>  "Saint Joseph Hospital - Atlanta",
    "28"=>  "University of Louisville",
    "29"=>  "Floyd Medical Center",
    "44"=>  "Johnston Health Hospital",
    "33"=>  "GEA Physician Group",
    "36"=>  "Wayne Memorial Hospital",
    "39"=>  "Emergent Medical Associates",
    "42"=>  "NES Physician Group",
    "45"=>  "Medical Center of Central Georgia",
    "32"=>  "Henry Medical Center",
    "25"=>  "Piedmont-Newnan Hospital",
    "31"=>  "Team Health Second PL",
    "9"=>   "Piedmont Hospital",
    "19"=>  "Emcare Inc   ",
    "49"=>  "HMA HOSPITAL SYSTEM",
    "1"=>   "Northeast Georgia Medical Center",
    "22"=>  "North American Credit Services",
    "27"=>  "North American Credit Services VBO",
    "30"=>  "Phoebe Putney Memorial Hospital",
    "32"=>  "Henry Medical Center",
    "38"=>  "St. Mary Hospital - Athens",
    "50"=>  "Schumacher Group"); 
#-------------------------------------------------------------

#----------------------------------------------
#   sub read_cust_name()
#   access to srv6 db for cust infos
#   Argument: no
#              
#   no return;
#----------------------------------------------
sub read_cust_name()
{
    my $dbh = DBI->connect("dbi:Pg:dbname=$db;host=$hostname",
    "$user", "$password") or die "Can't connect to postgres db: $!\n";
    
    my $sth  = $dbh->prepare("select distinct cust_id, cust_business_name from cust_infos order by cust_id;");
    my $rv = $sth->execute;
    print "ok";
    while ( my $ref = $sth->fetchrow_arrayref() )
    {
        $org_hash{${$ref}[0]}=${$ref}[1];
        print "${$ref}[1]";
    }
    
    $sth->finish();
    $dbh->disconnect();
 
}



#----------------------------------------------
# void write_backup$($ref_hash)
#   write srv1 data to backup file
#   Argument: $ref_hash   reference of the hash
#             $hash_name
#   no return;
#----------------------------------------------
sub write_backup($$)
{
    my $ref_hash = shift;
    my $hash_name = shift;
    my $file_name ="$DIR/save/email_server_backup.txt";
    
    open(BU, ">>$file_name.tmp");
    
    foreach my $key (keys %{$ref_hash})
    {
        
        print BU "$hash_name ";
        
        if ( scalar(keys %{$ref_hash->{$key}} ) == 8 ) 
        {
            my ( $cc, $oo, $fn, $dp, $reci, $se, $pi, $ot) =
                 (  $ref_hash->{$key}->{Cid},
                    $ref_hash->{$key}->{Oid},
                    $ref_hash->{$key}->{Filename},
                    $ref_hash->{$key}->{Description},
                    $ref_hash->{$key}->{Recipient},
                    $ref_hash->{$key}->{Sent_Time},
                    $ref_hash->{$key}->{OpenBy},
                    $ref_hash->{$key}->{PickupTime}     
                    );
            $reci =~ s/\,/<br>/g;
            print BU "<key>$key<key><num>8<num>";
            print BU "<cid>$cc<cid><oid>$oo<oid><fn>$fn<fn><dp>$dp<dp><rp>$reci<rp><se>$se<se><pi>$pi<pi><ot>$ot<ot>\n";
         
        }
        else
        {
            my ( $cc, $oo, $fn, $dp, $reci, $se ) =
                 (  $ref_hash->{$key}->{Cid},
                    $ref_hash->{$key}->{Oid},
                    $ref_hash->{$key}->{Filename},
                    $ref_hash->{$key}->{Description},
                    $ref_hash->{$key}->{Recipient},
                    $ref_hash->{$key}->{Sent_Time}  
                    );  
            print BU "<key>$key<key><num>6<num>";
            print BU "<cid>$cc<cid><oid>$oo<oid><fn>$fn<fn><dp>$dp<dp><rp>$reci<rp><se>$se<se>\n";
        }
    }
    
    close(BU);
    
    if($hash_name eq "score")
    {
        unlink $file_name || die "Error: remove $file_name failed!\n";
        
        my $cmd = "mv $file_name.tmp $file_name";
        while( system($cmd) )
        {
            if($DEBUG == 1)
            {
                print $cmd," failed!\n";
            }
        }
    }
    
}

#----------------------------------------------
# void read_backup$($ref_hash)
#   read srv1 backup data to hash
#   Argument: $name   hash table name
#   no return;
#----------------------------------------------
sub read_backup($$$)
{
    my $ref_hash = shift;
    my $name = shift;
    my $file_name = shift;
    
    if($name ne "score" && $name ne "payerintel")
    { if($DEBUG == 1)
        {
            print "Error, check hash name;\n";
        }
    }
    
    open(BU, "<$DIR/save/$file_name")|| die "cannot open file\n";
        
        
    while(my $line = <BU>)
    {
        if($line =~ /$name\s+.*<num>8<num>/)
        {
            $line =~ /<key>(.*)<key>/;
            my $key = $1;
            $key =~ s/\s*//g;

            if($line =~ /<cid>(.*)<cid><oid>(.*)<oid><fn>(.*)<fn><dp>(.*)<dp><rp>(.*)<rp><se>(.*)<se><pi>(.*)<pi><ot>(.*)<ot>/s)
            {
                (
                $ref_hash->{$key}->{Cid},
                $ref_hash->{$key}->{Oid},
                $ref_hash->{$key}->{Filename},
                $ref_hash->{$key}->{Description},
                $ref_hash->{$key}->{Recipient},
                $ref_hash->{$key}->{Sent_Time},
                $ref_hash->{$key}->{OpenBy},
                $ref_hash->{$key}->{PickupTime}
                )   = ($1,$2,$3,$4,$5,$6,$7,$8);    
            }
            else
            {
                print "ERROR: not match!";
            }
        }   
        
        if($line =~ /$name\s+.*<num>6<num>/)
        {
            $line =~ /<key>(.*)<key>/;
            my $key = $1;
            $line =~ /<cid>(.*)<cid><oid>(.*)<oid><fn>(.*)<fn><dp>(.*)<dp><rp>(.*)<rp><se>(.*)<se>/;
            
            (   $ref_hash->{$key}->{Cid},
                $ref_hash->{$key}->{Oid},
                $ref_hash->{$key}->{Filename},
                $ref_hash->{$key}->{Description},
                $ref_hash->{$key}->{Recipient},
                $ref_hash->{$key}->{Sent_Time}
            )   = ($1,$2,$3,$4,$5,$6);  
        }       
    }
    
        close(BU);
}

#-------------------------------------------------------------
# $ get_sleeptime(void)
#       Calculate the sleep time (set the running time at 7:30am)
#       Arg: no
#
#       return $sleeptime (seconds);
# ------------------------------------------------------------
sub get_sleeptime()
{
    my $now = time();
    my ($hour, $day, $mon, $yr) = (localtime($now))[2,3,4,5];
    if ($hour > 17 ) {
        ($day, $mon, $yr) = (localtime(24 * 3600 + $now))[3,4,5];
        my $tom = timelocal(1,30,7,$day, $mon, $yr);
        return ($tom - $now);
    }
    else {
        return (3600);
    }
}

#-------------------------------------------------------------
# $ read_screen_payerintel_files_list(void)
#       read screen files list for record file
#           and save them to @screen_score_files_list
#       Arg: no
#
#       return no;
# ------------------------------------------------------------
sub read_screen_payerintel_files_list()
{
    my $payerintel_name = "$screen_files_directory/$screen_payerintel_files_name";
    
    # check if the file exists
    if (-e $payerintel_name )
    {
        open(FH,"<$payerintel_name");
        while (<FH>) 
        {
            my $file_name = $_;
            $file_name =~ /^(\S*)\s*overwritten/;
            $file_name = $1;
            if($file_name)
            {
                $screen_payerintel_files_list{$file_name} = 1;
            }
        }
        close(FH);
    }
}

#-------------------------------------------------------------
# $ read_screen_score_files_list(void)
#       read screen files list for record file
#           and save them to @screen_files_list
#       Arg: no
#
#       return no;
# ------------------------------------------------------------
sub read_screen_score_files_list()
{
    my $score_name = "$screen_files_directory/$screen_score_files_name";
    
    # check if the file exists
    if (-e $score_name && -s $score_name)
    {
        open(FH,"<$score_name");
        while (<FH>) 
        {
            my $file_name = $_;
            $file_name =~ /^(\S*)\s*overwritten/;
            $file_name = $1;
            if($file_name)
            {
                $screen_score_files_list{$file_name} = 1;
            }
        }
        close(FH);
    }
}

#-------------------------------------------------------------
# void send_emails($$)
#       Send  mails to every receiver
#        
#
#       No value returned
# ------------------------------------------------------------
sub send_emails($$)
{
    my $addrs = shift;
    my $content  = shift;

    my @emails = split( /[,;' ']/, $addrs);
    return if @emails == 0;
    my $receiver = shift @emails ;
    my $cc = (@emails > 0)?  join ',', @emails : "";

    my $msg = MIME::Lite->new(
            From    => 'support@medlytix.com',  
            To      => $receiver,
            Bcc     => $cc,
            Subject => 'Reminder',
            Data    => $content
            );
    return if ! $msg;
    $msg->send('smtp', 'mailhost', Timeout => 600);
}


#-------------------------------------------------------------
# @ get_IDs(@)
#       Retrieve the CID & OID
#       Arg: @subject 
#
#       return ($CID, $OID);
# ------------------------------------------------------------
sub get_IDs($)
{
    my $subject = shift;
    my $CID = 0;
    my $OID = 0;
    
    if( $subject =~ /\((\d+)_(\d+)_/ )
    {
        return ($1, $2);
    }
    else
    { 
        if( $DEBUG == 1)
        {
            print "Weried Email subject: $subject\n"
        }
        return ($CID, $OID);
    }
}

#-------------------------------------------------------------
# @ get_IDs(@)
#       Retrieve the CID & OID
#       Arg: @subject 
#
#       return ($CID, $OID);
# ------------------------------------------------------------
sub get_IDs_score($)
{
    my $subject = shift;
    my $CID = 0;
     
    if( $subject =~ /batch\s*(\d+).*\s*processed/ )
    {
        $CID = $1;
        
        return $CID;
    }
    else
    { 
        if( $DEBUG == 1)
        {
            print "Weried Email subject: $subject\n"
        }
        return  $CID;
    }
}

#-------------------------------------------------------------
# $ get_option(@)
#       Retrieve the sending alert option
#       Arg: @subject 
#
#       return $option (0 or 1, 0 sent, 1 not sent);
# ------------------------------------------------------------
sub get_option($)
{
    my $subject = shift;
    my $option = 0;
    
    if( $subject =~ /\(\d+_\d+_(\d)_\d+/ )
    {
        return $1;
    }else
    {
        return $option;
    }
    
}


#-------------------------------------------------------------
# $ get_key(@)
#       Retrieve a key for file_record
#       Arg: @subject 
#
#       return a key($cid_$oid_$date);
# ------------------------------------------------------------
sub get_key($)
{
    my $subject = shift;

    if ( $subject =~ /\((\d+_\d+_\S+)\)/ )
    {
        return $1;
    }
    return ''; 
}

#-------------------------------------------------------------
# $ get_key_score(@)
#       Retrieve a key for file_record
#       Arg: @subject 
#
#       return a key($cid_$oid_$date);
# ------------------------------------------------------------
sub get_key_score($)
{
    my $subject = shift;

    if ( $subject =~ /batch\s*(\d+_\d+.*)\s*processed/ )
    {
        my $key = $1;
        $key =~ s/\s*//g;
        return $key;
    }
    return ''; 
}

#-------------------------------------------------------------
# $ get_pickup_time($)
#       search the argument for pickup time
#       Arg: $string
#
#       return $pickuptime;
# ------------------------------------------------------------
sub get_pickup_time($)
{
    my $ptime = shift;
     
    if ( $ptime =~ /Opened on:\s+/ )
    {
        $ptime = $';
        
        $ptime =~ /\n/;
        $ptime = $`;
        $ptime =~ s/[,|@|\(|\)]//g;
     
        $ptime =~ s/^(\S+) (\S+)/$2 $1/;
        return $ptime;
    } 
     
    return '';
}

#-------------------------------------------------------------
# $ calculate_padding_number($)
#       Calculate the padding number of base64 string
#       Arg: $string
#
#       return $padding_number;
# ------------------------------------------------------------
sub calculate_padding_number($)
{
    my $string = shift;
    my $count = 0;
     
    $string =~ s/\s+//g;
    $count = length($string);
    
    if( $count % 4  == 0 )
    {
        return 0;
    } 
    else
    {
        return ( 4 - ( $count % 4 ) );
    }   
}

#------------------------------------------------------------
# void get_notifications($$)
#       check the mail box
#       insert wanted emails into %file_record
#       Arg:
#           $head_information (email head) 
#           $email_body (email body)
#       No value returned
# -----------------------------------------------------------
sub get_notifications($$)
{
    my $head_information = shift;
    my $email_body = shift;
        
    my $sub = join('', 
            @{$head_information->{Subject}});   
    
                
    # decode the base64 string      
    if ( $email_body  =~ m/boundary/  && 
        $email_body  =~ m/base64/)
    {
        my $pos = index($email_body , 'Encoding: base64');
        $email_body  = substr($email_body , $pos + 16);
        #$email_body  =~ s/^\s*//;
        $pos = index($email_body , "---");
        $email_body  = substr($email_body , 0, $pos);
        $email_body  =~ s/\s*$//;
        my $padding_number = calculate_padding_number($email_body);
        for( ;$padding_number > 0; $padding_number-- )
        {
            
            $email_body = $email_body."=";
        }
        $email_body  = decode_base64($email_body );
    }       
                
    # get the hash key from the email's subject
    my $key = get_key($sub);    
        
    # if the original email exists in hash,
    # and it is the first response, insert notification
    if( $key )
    {
        
        if( defined $file_record{$key} && 
            scalar(keys %{$file_record{$key}} ) == 8 )
        {
            return ;        
        }
        
        # get the email account who opened the original email 
        my $whopickup = join('', @{$head_information->{From}});
        
        # get open time
        # if no time in the email body,use sent time of the notification 
        my $pickuptime = get_pickup_time($email_body);
        if( !$pickuptime )
        {
            $pickuptime = join('', 
                            @{$head_information->{Date}});  
            $pickuptime =~ /^(.*:.*:\d\d)/;
            $pickuptime = $1;
        }
    
        # if no key found in hash %file_record,
        # insert the reference of the created %email_properties;
        # if a key found, derectly insert values 
        if( !exists $file_record{$key} )
        {
            my %email_properties = ();
                            
            # get the machine time of the pickupttime
            if( str2time($pickuptime) )
            {
                $email_properties{PickupTime} = str2time( $pickuptime );
            }else
            {
                $email_properties{PickupTime} = 0;
                if( $DEBUG == 1)
                {
                    printf "Pickup time $pickuptime cannot be converted\n";
                    printf "subject: $sub \n";
                }
            }
            
            # get the name of who opened the file
            $email_properties{OpenBy} = $whopickup;
            
            # insert the hash reference into hash %file_record
            $file_record{$key} = \%email_properties;
        }else 
        {
            if( scalar(keys %{$file_record{$key}} ) < 8 )
            {
                # get the machine time of the pickupttime
                if( str2time($pickuptime) )
                {
                    $file_record{$key}->{PickupTime} = str2time( $pickuptime );
                }else
                {
                    $file_record{$key}->{PickupTime} = 0;
                    if( $DEBUG == 1)
                    {
                        printf "Pickup time $pickuptime cannot be converted\n";
                        
                        printf "subject: $sub \n";          
                    }
                }
                
                # get the name of who opened the file
                $file_record{$key}->{OpenBy} = $whopickup;
            }
        }
    }
    
}





#------------------------------------------------------------
# void check_email_account(void)
#       check the mail box
#       insert wanted emails record into %file_record
#
#       No value returned
# -----------------------------------------------------------
sub check_email_account()
{
    
    
    my $counter =   2;  # we have two mail servers right now
    my $server;
    my $email;
    my $imap;
    
    while($counter > 0)
    {
        if($counter == 2)
        {
#           $server =   "srv1"; 
#           $email = $email_account;
            read_backup(\%file_record,"payerintel","srv1_backup.txt");
            read_backup(\%file_record,"payerintel","email_server_backup.txt");
            if($DEBUG == 1)
            {
                print "read files end!\n";
                
            }
            $counter--;
            next;
        }
        else
        {
            
            $server="exch2007.plexnex.net";
            $email = $email_account_new;
        }
        
        $counter--;
         
        $imap = Mail::IMAPClient->new(
                    Server =>   $server,
                    User   => $email,
                    Password=> $email_passwd,
                    Port => 993,       # optional
                    Ssl => 1,
            )|| next;
    
        $imap->select("INBOX");
        
        
        my $criteria = "SENTON $today Subject \"PayorIntel\"";  
        my @msgs = $imap->search($criteria);
        foreach my $msg (@msgs) 
        {
            my $hashref = $imap->parse_headers($msg,
                "Date","Received","Subject","To", "Cc","Bcc", "From");   
                
            my $body_string = $imap->body_string($msg); 
            if(!$body_string)
            {
                next;
            }
            $body_string =~ tr{\n}{ };
            $body_string =~ s/\=/ /g;

            my $sub = join('', @{$hashref->{Subject}}); 
        
        
            # filter the junk emails based on the subject
            if( $sub !~ /PayorIntel\s+Report/i || 
                $sub =~ /AutoReply/i || 
                $sub =~ /Automatic reply/i ) 
            {
                next;
            }   
        
            # Original emails pattern
            if( $sub =~ /^\s*Encrypt/ || 
                $sub =~ /^\s*PayorIntel/ ||
                $sub =~ /^\s*Medlytix/ )
            {
                get_origin_emails( $hashref, $body_string );
            }else 
            {
                # Notification emails pattern
                if( $sub =~ /^\s*(RECEIPT|RE:)/ )
                {
                    get_notifications( $hashref,$body_string );
                }
                else
                {
                    if( $DEBUG == 1 )
                    {
                        print "Warning: Weired email subject: $sub.\n";
                        print "No Idea where to go!\n\n";
                    }
                }
            }
        }
        $imap->close;
    }
}


#------------------------------------------------------------
# void check_email_account_score(void)
#       check the mail box
#       insert wanted emails record into %file_record
#
#       No value returned
# -----------------------------------------------------------
sub check_email_account_score()
{
    
    
    my $counter =   2;  # we have two mail servers right now
    my $server;
    my $imap;
    my $email;
    
    while($counter  >   0)
    {
        if($counter == 2)
        {
#           $server =   "srv1";
#           $email = $email_account;
            read_backup(\%file_record_score,"score","srv1_backup.txt");
            read_backup(\%file_record_score,"score","email_server_backup.txt");
            $counter--;
            next;
        }
        else
        {
             $server    =   "exch2007.plexnex.net";
             $email = $email_account_new;
        }
        
        $counter--;
        $imap = Mail::IMAPClient->new(
                 Server =>   $server,
                 User   => $email,
                Password=> $email_passwd,
                 Port => 993,       # optional
                 Ssl => 1,
                     ) or next;
             
        
        $imap->select("INBOX");
            
        
        my $criteria = "SENTON $today Subject \"batch\"";   
        my @msgs = $imap->search($criteria);
        foreach my $msg (@msgs) 
        {
            my $hashref = $imap->parse_headers($msg,
                "Date","Received","Subject","To", "Cc","Bcc", "From");   
                
            my $body_string = $imap->body_string($msg); 
            if(!$body_string)
            {
                next;
            }
            $body_string =~ tr{\n}{ };
            $body_string =~ s/\=/ /g;

            my $sub = join('', @{$hashref->{Subject}}); 
        
        
            # filter the junk emails based on the subject
            if( $sub !~ /^\s*Medlytix/ ||
                $sub !~ /processed/ || 
                $sub !~ /batch/ ||
                $sub !~ /score/ ||
                $sub !~ /\d+_\d+/)
            {
                next;
            }   
     
            
            get_origin_emails_score( $hashref, $body_string );

        }
        $imap->close;
    }
}

#------------------------------------------------------------
# void get_origin_email($$)
#       check the mail box
#       insert wanted emails into %file_record
#       Arg:
#           $head_information (email head) 
#           $email_body (email body)
#       No value returned
# -----------------------------------------------------------
sub get_origin_emails($$)
{
        my $head_information = shift;
        my $email_body = shift;
        my $file_name;
        my $file_description;
        
        # get the subject of the email
        my $sub = join('', @{$head_information->{Subject}});        
        
        # get sent time from [Date]
        my $senttime = join('', 
                            @{$head_information->{Date}});  
        $senttime =~ s/^.*,\s+//;
        
        # get recipients from [TO] [CC] and [BCC]
        # filter recipients with medlytix.com as subfix
        my @recipients = @{$head_information->{To}};    
        foreach my $cc (@{$head_information->{Cc}})
        {
            push(@recipients, $cc);
        }
        foreach my $bcc (@{$head_information->{Bcc}})
        {
            push(@recipients, $bcc);
        }
         
        @recipients = grep { $_ !~ /medlytix.com/ } @recipients;
        my $recipient = join(',', @recipients);
        
        if( $recipient =~ /\"\S*\"/ && $recipient =~ /</g)
        {
            $recipient =~ s/\"\S*\"//g
        }
        
        $recipient =~ s/[<|>]//g;
        $recipient =~ s/\s//g;
        if (!$recipient)    # first email for testing new company
        {
            $recipient = "Nobody";
        }
        
        #get the key for hash %file_record, and get $cid $oid
        my $key = get_key($sub);
        my ( $cust_id,  $oid) = get_IDs($sub);
        if( $cust_id == 0 && $oid == 0 )
        {
            if($DEBUG == 1)
            {
                print "Error in dealing with the original emails,
                        CID and OID of $sub cannot be determined!\n";
                sleep 100;
            }
            return;
        }

        # get file name and file description
        if( $key && scalar(keys %{$file_record{$key}} ) < 8) 
        {               
            # search the fn and description from two different type files
            if ( $email_body =~ 
                /^(.*)report\s+file\s+\((.+)\)\s+ready/  )
            {
                # for files without attachement 
                if(!defined($file_record{$key}->{Filename}))
                {
                    $file_name = $2;
                    $file_name =~ s/\cM//g;
                    $file_name =~ s/\s*//g;
                    $file_name =~ s/[\r|\n|\s]//g;
                }
                
                if(!defined($file_record{$key}->{Description}))
                {
                    $file_description = $1;
                }
                if( $DEBUG == 1)
                {
                    print "Email Body:\n $email_body\n";
                }   
            }else
            {
                # for files with attachment
                if ( $email_body =~ 
                    /.*\((.+)\)\s+for\s+(.+)\s+attached/ )
                {
                    ($file_name, $file_description) = ( $1, $2 );
                    
                    $file_name =~ s/\cM//g;
                    $file_name =~ s/\s*//g;
                    $file_name =~ s/[\r|\n|\s]//g;
                    $file_description =~ s#\=# #g;  
                    $file_description =~ s/\cM//g;
                    $file_description =~ s/\s*$//g;
                    
                    if($recipient =~ /Nobody/ &&
                       $email_body =~ /Original\s+receipts:\s+(.*)/s)
                    {
                        
                        $recipient = $1;
                        print "$recipient\n";
                        $recipient =~ s/\s//gs;
                        $recipient =~ s/;/,/gs;
                        $recipient =~ s/UID.*$//;
                        print "$recipient\n";
                    }
                }else
                {
                    
                    if( $sub !~ "Encrypt")
                    {
                        
                        if(!defined($file_record{$key}->{Filename}))
                        {
                            $file_name = "No Report"; #push filename
                        }
                
                        if(!defined($file_record{$key}->{Description}))
                        {
                            $file_description = "No Report"; #push description              
                        }       
                        
                    }else
                    {
                        if(!defined($file_record{$key}->{Filename}))
                        {
                            $file_name = "unknown"; #push filename
                        }
                
                        if(!defined($file_record{$key}->{Description}))
                        {
                            $file_description = "unknown"; #push description    
                        }   
                    }
                }
            }
        }
        
        
        # No key exists, insert a new hash reference into hash %file_record
        if( !exists($file_record{$key})  )
        {
            my %email_properties = ();
                            
            $email_properties{Cid} = $cust_id;
            $email_properties{Oid} = $oid;
            $email_properties{Filename} = $file_name;
            $email_properties{Description} = $file_description;
            $email_properties{Recipient} = $recipient;
            if( str2time( $senttime ) )
            {
                $file_record{$key}->{Sent_Time} = str2time($senttime);
            }else
            {
                $file_record{$key}->{Sent_Time} = 0;
                if( $DEBUG == 1)
                {
                    printf "Sent time $senttime cannot be converted\n";
                }
            }
            $file_record{$key} = \%email_properties; 
        }else
        {
            # the key already exists, insert values into %$file_record{$key}    
            if(!defined($file_record{$key}->{Cid}))
            {
                $file_record{$key}->{Cid} = $cust_id;
            }
            if(!defined($file_record{$key}->{Oid}))
            {
                $file_record{$key}->{Oid} = $oid;
            }
            
            if(!defined($file_record{$key}->{Filename}))
            {
                $file_record{$key}->{Filename} = $file_name;
            }
            
            if(!defined($file_record{$key}->{Description}))
            {
                $file_record{$key}->{Description} = $file_description;
            }
            
            if(!defined($file_record{$key}->{Recipient}))
            {
                $file_record{$key}->{Recipient} = $recipient;
            }
            
            if(!defined($file_record{$key}->{Sent_Time}))
            {
                
                if( str2time( $senttime ) )
                {
                    $file_record{$key}->{Sent_Time} = str2time($senttime);
                }else
                {
                    $file_record{$key}->{Sent_Time} = 0;
                    if( $DEBUG == 1)
                    {
                        printf "Sent time $senttime cannot be converted\n";
                    }
                }
            }
        
            
            
        }
}

#------------------------------------------------------------
# void get_origin_email_score($$)
#       check the mail box
#       insert wanted emails into %file_record
#       Arg:
#           $head_information (email head) 
#           $email_body (email body)
#       No value returned
# -----------------------------------------------------------
sub get_origin_emails_score($$)
{
        my $head_information = shift;
        my $email_body = shift;
        my $file_name;
        my $file_description;
        
        # get the subject of the email
        my $sub = join('', @{$head_information->{Subject}});        
        
        # get sent time from [Date]
        my $senttime = join('', 
                            @{$head_information->{Date}});  
        $senttime =~ s/^.*,\s+//;
        
        # get recipients from [TO] [CC] and [BCC]
        # filter recipients with medlytix.com as subfix
        my @recipients = @{$head_information->{To}};    
        foreach my $cc (@{$head_information->{Cc}})
        {
            $cc =~ s/\".*\"//g;
            push(@recipients, $cc);
        }
        foreach my $bcc (@{$head_information->{Bcc}})
        {
            $bcc =~ s/\".*\"//g;
            push(@recipients, $bcc);
        }
        @recipients = grep { $_ !~ /medlytix.com/ } @recipients;
        my $recipient = join(',', @recipients);
        $recipient =~ s/\"\S*\"//g;

        $recipient =~ s/[<|>]//g;
        $recipient =~ s/\s//g;
        if (!$recipient)    # first email for testing new company
        {
            $recipient = "Nobody";
        }
        
        #get the key for hash %file_record, and get $cid
        my $key = get_key_score($sub);

        $sub =~ /processed;\s*(.*)\s*score/;
        
        $file_description = $1;
        if(!$file_description)
        {
            if($DEBUG == 1)
            {
                print "$sub\n";
                sleep 100;
            }   
        }
        else
        {
            $file_description =~ s/\s*//g;
        }
        my $cust_id= get_IDs_score($sub);
        if( $cust_id == 0 )
        {
            if($DEBUG == 1)
            {
                print "Error in dealing with the original emails,
                        CID  of $sub cannot be determined!\n";
            }
            return;
        }
        
        # get file name and file description
        if( $key && scalar(keys %{$file_record_score{$key}} ) < 8) 
        {   
            # search the fn from two different type files
            if ( $email_body =~ 
                /^.*Filename\s+:\s+(.+)\s*File\s*size/g  )
            {
                # for files without attachement 
                if($1)
                {
                    $file_name = $1;
                    
                    $file_name =~ s/\cM//g;
                    $file_name =~ s/\s*//g;
                    $file_name =~ s/[\r|\n|\s]//g;
                }
                else
                {
                    if( $DEBUG == 1)
                    {
                        print "Email Body:\n $email_body\n";
                    }   
                }
                
                
            }else
            {
                if( $DEBUG == 1)
                {
                    print "Email Body:\n $email_body\n";
                }
            }
        }
        
        # No key exists, insert a new hash reference into hash %file_record
        if( !exists($file_record_score{$key})  )
        {
            my %email_properties = ();
                            
            $email_properties{Cid} = $cust_id;
            $email_properties{Oid} = 0;
            $email_properties{Filename} = $file_name;
            $email_properties{Description} = $file_description;
            $email_properties{Recipient} = $recipient;
        
            if( str2time( $senttime ) )
            {
                $file_record_score{$key}->{Sent_Time} = str2time($senttime);
            }else
            {
                $file_record_score{$key}->{Sent_Time} = 0;
                if( $DEBUG == 1)
                {
                    printf "Sent time $senttime cannot be converted\n";
                }
            }
            $file_record_score{$key} = \%email_properties; 
        }else
        {
            # the key already exists, insert values into %$file_record{$key}    
            $file_record_score{$key}->{Cid} = $cust_id;
            $file_record_score{$key}->{Oid} = 0;
            if(!defined $file_record_score{$key}->{Filename})
            {
                $file_record_score{$key}->{Filename} = $file_name;
            }
            
            $file_record_score{$key}->{Description} = $file_description;
            $file_record_score{$key}->{Recipient} = $recipient;
            if( str2time( $senttime ) )
            {
                $file_record_score{$key}->{Sent_Time} = str2time($senttime);
            }else
            {
                $file_record_score{$key}->{Sent_Time} = 0;
                if( $DEBUG == 1)
                {
                    printf "Sent time $senttime cannot be converted\n";
                }
            }
            
        }
}


#------------------------------------------------------------
# $ search_file_name($)
#       To go through the file_record to find the specific filename
#       Argument: file name
#
#       Return $key;  (The specific key pointing to the filename)
# -----------------------------------------------------------
sub search_file_name($)
{
    my $filename = shift;
    $filename =~ /<(.*)>/;
    $filename = $1;
    my @keys = ();
    foreach my $key (keys %file_record)
    {
         
        my $fn = $file_record{$key}->{Filename};
        if($fn)
        {
            $fn =~ s/ //g;
            if( $fn eq $filename )
            {   
                 push @keys, $key;
                # return $key;
            }
        }
        
    }
    return @keys;
}


#------------------------------------------------------------
# $ search_file_name_score($)
#       To go through the file_record_score to find the specific filename
#       Argument: file name
#
#       Return $key;  (The specific key pointing to the filename)
# -----------------------------------------------------------
sub search_file_name_score($)
{
    my $filename = shift;
    $filename =~ /<(.*)>/;
    $filename = $1;
    my @keys = ();
     
    foreach my $key (keys %file_record_score)
    {
         
        my $fn = $file_record_score{$key}->{Filename};
        if($fn)
        {
            $fn =~ s/( |\n|\r)//g;
             
            $filename =~ s/( |\n|\r)//g;
            
            if( $fn eq $filename )
            {   
                
                 push @keys, $key;
                # return $key;
            }
        }
        
    }
    return @keys;
}


#------------------------------------------------------------
# sub save_message($)
#       To save the sending message to backup file 
#       Argument: message content
#
#       Return no
# -----------------------------------------------------------
sub save_message($$) 
{
    my $msg = shift;
    my $file = shift;
    my $date_str = localtime;

    open(LOGF, ">>$screen_files_directory/$file");
    print LOGF "$msg\@$date_str\n";
    close LOGF;
}


#------------------------------------------------------------
# sub update_dashboard()
#       To check if this file has been received 
#       If so, send info to dashboard
#
#       Return no
# -----------------------------------------------------------
sub update_dashboard()
{
    my $previous_files_not_picked_up = "$screen_files_directory/unpickedup_files.txt";
    
    if (-e $previous_files_not_picked_up &&
        -s $previous_files_not_picked_up)
    {
        my %part_of_files_not_picked_up =   ();
        my %send_lists  =   ();
        open(FH,"<$previous_files_not_picked_up");
        
        while (<FH>) 
        {
            my $one_line    = $_;
            $one_line   =~ /^(.*)!!!/;
            my $file_name = $1;
            $one_line   =~ /^.*!!!(.*)/;
            my $part_of_file_name = $1;
            $one_line =~ /^.*payorintel_(.*)\.report/;
            $part_of_file_name = $part_of_file_name."|".$1;
            
            if(!exists $payerintel_files_not_picked_up{$file_name})
            {
                if(!exists $send_lists{$part_of_file_name})
                    {
                        $send_lists{$part_of_file_name} = 1;
                    }
                
            }else
            {
                    $send_lists{$part_of_file_name} = 0;
            }
        }
        close(FH);
        
        foreach my $key (keys %send_lists)
        {
            
            sleep 5;
            if($send_lists{$key} == 1)
            {
                my $content = "900|".$key."|dum|".time."|emtrack";
                
                #   check if send message succesfully
                if(send_one_msg($content)   ==  0)
                {
                    save_message($content,$dashboard_message_backup);
                }
                else
                {
                    save_message($content, "db_messages_succ.txt");
                }
            }
        }   
    }
    
    if(%payerintel_files_not_picked_up)
    {
        open(FH,">$previous_files_not_picked_up.tmp");
        foreach my $key (keys %payerintel_files_not_picked_up)
        {
            print FH $key;
            print FH "!!!";
            print FH $payerintel_files_not_picked_up{$key};
            print FH "\n";
        }
        close(FH);  
    }
    
    
    # remove the previous one if it exists
    if (-e $previous_files_not_picked_up)
    {
#       my $date = get_datestamp();
        my $bak_file = $previous_files_not_picked_up.".bak";
        my $timer = get_timestamp();
        
        # trim log file if file size > 100K
        if(-e $bak_file && ((-s $bak_file) > 100000) )
        {
            my $cmd = "tail -500 $bak_file > $bak_file.tmp";
            while( system($cmd) )
            {
                if($DEBUG == 1)
                {
                    print $cmd," failed!\n";
                }
            }
            $cmd = "cp $bak_file.tmp  $bak_file";
            while( system($cmd) )
            {
                if($DEBUG == 1)
                {
                    print $cmd," failed!\n";
                }
            }
            unlink "$bak_file.tmp";
        }
        
        my $cmd = "echo $timer >> $bak_file";
        while( system($cmd) )
        {
            if($DEBUG == 1)
            {
                print $cmd," failed!\n";
            }
        }
        $cmd = "cat $previous_files_not_picked_up >> $bak_file";
        while( system($cmd) )
        {
            if($DEBUG == 1)
            {
                print $cmd," failed!\n";
            }
        }
    }
    
    
    if( -e  "$previous_files_not_picked_up.tmp")
    {
        my $cmd = "mv $previous_files_not_picked_up.tmp $previous_files_not_picked_up";
        while( system($cmd) )
        {
            if($DEBUG == 1)
            {
                print $cmd," not success!\n";
            }
        }
    }
}

#------------------------------------------------------------
# sub send_one_msg($)
#       send msg to dashboard
#
#       Return no
# -----------------------------------------------------------
sub send_one_msg {
     my $MSG = shift;

     my $iaddr = inet_aton("srv6");
     my $paddr = sockaddr_in(5005, $iaddr);
     my $proto = getprotobyname("tcp");


     my $now = ` /bin/date `;

     my $ret = socket(SOCK, PF_INET, SOCK_STREAM, $proto) ;
     if ( ! $ret ) {
         return 0;
     }

     $ret = connect(SOCK, $paddr);
     if ( ! $ret ) {
        return 0;
     }

     select SOCK; $|=1;

     my $feedback="";
     while( $feedback !~ /^ACK/ ) {
         print SOCK "$MSG\n";
         $feedback = <SOCK> ;
         sleep 10 if $feedback !~ /^ACK/;
     }

     close (SOCK);
     sleep 1;
     select STDOUT;
     return 1;
}


#------------------------------------------------------------
# $ is_received_by_another_email($)
#       To check if this file has been received by another email
#       Argument: file name
#
#       Return $key;  (The specific key pointing to the filename)
# -----------------------------------------------------------
sub is_received_by_another_email($)
{
    my $filename = shift;
    if($filename)
    {
        my @keys = ();
        foreach my $key (keys %file_record)
        {
         
            my $fn = $file_record{$key}->{Filename};
            if($fn)
            {
                $fn =~ s/ //g;
                if( $fn eq $filename &&
                    scalar(keys %{$file_record{$key}})  == 8 ) 
                {   
                    return 1;
                }
            }
     
        }
    }
     
    return 0;
}

#------------------------------------------------------------
# void get_ftp_email(void)
#       check the mail box
#       insert wanted emails into %file_record
#
#       No value returned
# -----------------------------------------------------------
sub get_ftp_email()
{
    my $counter =   2;  # we have two mail servers right now
    my $server;
    my $imap;
    my $email;
    
    while($counter  >   0)
    {
        if($counter == 2)
        {
#           $server =   "srv1";
#           $email = $email_account;
             $server    =   "NULL"; 
             $counter--;
             next;
        }
        else
        {
             $server    =   "exch2007.plexnex.net";
             $email = $email_account_new;
        }
        
        $counter--;
        $imap = Mail::IMAPClient->new(
                 Server =>   $server,
                 User   => $email,
                Password=> $email_passwd,
                 Port => 993,       # optional
                 Ssl => 1,
                     ) or next;
                     
        $imap->select("INBOX");
     
            
        my $criteria = "SENTON $today Subject \"FTP\"";
        my @msgs = $imap->search($criteria);
        foreach my $msg (@msgs) 
        {
            my $hashref = $imap->parse_headers($msg,
                "Date","Received","Subject","To", "Cc","Bcc", "From");    

            my $sub = join('', @{$hashref->{Subject}});
        

            #filter the junk emails based on the subject
            if ( $sub !~ /FTP\s+Server\s+Download\s+Tracking\s+Report/ ) 
            {
                next;
            };
            
            my $string = $imap->body_string($msg);
            $string =~ tr{\n}{ };
            my @records = split(/ /,$string);
            chomp(@records);
            foreach my $record (@records)
            {
                if ( $record !~ /,IP:/ )
                {
                    next;
                }
            
                my @row = split(/,/,$record);
                my @keys = search_file_name( $row[2] );
                if(@keys)
                {
                    foreach my $key (@keys) 
                    {
                        if ( $key && 
                             scalar(keys %{$file_record{$key}}) < 8 )
                        {
                            $file_record{$key}->{OpenBy} =  $row[1];
                            $file_record{$key}->{PickupTime} = $row[0];
                        }
                    }
                }
            }   
        }
        $imap->close;
    }
}


#------------------------------------------------------------
# void get_ftp_email_score(void)
#       check the mail box
#       insert wanted emails into %file_record
#
#       No value returned
# -----------------------------------------------------------
sub get_ftp_email_score()
{
    my $counter =   2;  # we have two mail servers right now
    my $server;
    my $imap;
    my $email;
    
    while($counter  >   0)
    {
        if($counter == 2)
        {
#           $server =   "srv1";
#           $email = $email_account;
             $server    =   "NULL"; 
             $counter--;
             next;
        }
        else
        {
             $server    =   "exch2007.plexnex.net";
             $email = $email_account_new;
        }
        
        $counter--;
        $imap = Mail::IMAPClient->new(
                 Server =>   $server,
                 User   => $email,
                Password=> $email_passwd,
                 Port => 993,       # optional
                 Ssl => 1,
                     ) or next;
        $imap->select("INBOX");
     
        
        my $criteria = "SENTON $today Subject \"FTP\""; 
        my @msgs = $imap->search($criteria);
        foreach my $msg (@msgs) 
        {
            my $hashref = $imap->parse_headers($msg,
                    "Date","Received","Subject","To", "Cc","Bcc", "From");    

            my $sub = join('', @{$hashref->{Subject}});
        

            #filter the junk emails based on the subject
            if ( $sub !~ /FTP\s+Server\s+Download\s+Tracking\s+Report/ ) 
            {
                next;
            };
            
            my $string = $imap->body_string($msg);
            $string =~ tr{\n}{ };
            my @records = split(/ /,$string);
            chomp(@records);
            foreach my $record (@records)
            {
                if ( $record !~ /,IP:/ )
                {
                    next;
                }
            
                my @row = split(/,/,$record);
                my @keys = search_file_name_score( $row[2] );
                if(@keys)
                {
                    foreach my $key (@keys) 
                    {
                        if ( $key && 
                         scalar(keys %{$file_record_score{$key}}) < 8 )
                        {
                            $file_record_score{$key}->{OpenBy} =  $row[1];
                            $file_record_score{$key}->{PickupTime} = $row[0];
                        }
                    }
                    }
            }   
        }
        $imap->close;
    }
}

#------------------------------------------------------------
# $ get_short_style_4time($)
#       Convert the GMT time to xx-xx-xx style
#       Argument: GMT time
#
#       Return $newtime;
# -----------------------------------------------------------
sub get_short_style_4time($)
{
    my $gmt = shift;
    if ( $gmt )
    {    
        my ( $sec, $min, $hour, $day, $mon, $yr ) = 
            (localtime( $gmt ))[0, 1, 2, 3,4, 5];
        my $newtime = sprintf("%02d/%02d/%2d<br>%02d:%02d:%02d",
            $mon+1,$day,$yr-100,$hour,$min,$sec);
        return $newtime;
        
    }else
    {
        return ' ';
    }
}

#------------------------------------------------------------
# $ get_datestamp()
#       Convert the GMT time to date
#       Argument: no
#
#       Return $newtime;
# -----------------------------------------------------------
sub get_datestamp()
{
    my ( $sec, $min, $hour, $day, $mon, $yr ) = 
            (localtime( time ))[0, 1, 2, 3,4, 5];
    my $newtime = sprintf("%02d%02d%02d",
            $mon+1,$day,$yr-100);
        return $newtime;
}

#------------------------------------------------------------
# sub get_sent_date()
#       To generate today's date like this format:
#       05-Mar-2007
#
#       Return $today;
# -----------------------------------------------------------
sub get_sent_date()
{
    my %months = ( 1=>"Jan",
                2=>"Feb",
                3=>"Mar",
                4=>"Apr",
                5=>"May",
                6=>"Jun",
                7=>"Jul",
                8=>"Aug", 
                9=>"Sep", 
                10=>"Oct", 
                11=>"Nov", 
                12=>"Dec"
                );
                
    my ( $day, $mon, $yr ) = 
            (localtime( time ))[3, 4, 5];
            
    my $today = sprintf("%02d-%s-%d",
            $day,$months{$mon+1},$yr-100+2000);
        return $today;
    
}

#------------------------------------------------------------
# $ get_timestamp()
#       Convert the GMT time to xx-xx-xx style
#       Argument: no
#
#       Return $newtime;
# -----------------------------------------------------------
sub get_timestamp()
{
    my ( $sec, $min, $hour, $day, $mon, $yr ) = 
            (localtime( time ))[0, 1, 2, 3,4, 5];
    my $newtime = sprintf("%02d%02d%02d%02d%02d%02d",
            $mon+1,$day,$yr-100,$hour,$min,$sec);
        return $newtime;
}

#------------------------------------------------------------
# $ get_date($)
#       Convert the GMT time to date
#       Argument: GMT time
#
#       Return $date;
# -----------------------------------------------------------
sub get_date($)
{
    my $gmt = shift;
    if ( $gmt )
    {    
        my ( $day, $mon, $yr ) = 
            (localtime( $gmt ))[3,4,5];
        my $newtime = sprintf("%02d/%02d/%2d",
            $mon+1,$day,$yr-100,);
        return $newtime;
        
    }else
    {
        return ' ';
    }
}

#------------------------------------------------------------
# $ is_same_date($$)
#       check if the two date are the same
#       Argument: $first_date
#                 $second_date
#
#       Return $date;
# -----------------------------------------------------------
sub is_same_date($$)
{
    my $first_date = shift;
    my $second_date = shift;
    if ( $first_date )
    {    
        my ( $day_first, $mon_first, $yr_first ) = 
            (localtime( $first_date ))[3,4,5];
        my ( $day_second, $mon_second, $yr_second ) = 
            (localtime( $second_date ))[3,4,5];
        if($day_first == $day_second &&
             $mon_first == $mon_second &&
             $yr_first == $yr_second)
        {
            return 1;
        }
        
    }else
    {
        return 0;
    }
}

#------------------------------------------------------------
# void send_alert_emails(void)
#       Send alter emails if the clients didn't open emails
#       in three days;
#
#       No value returned
# -----------------------------------------------------------
sub send_alert_emails()
{
    my $report_list = "Alert Report: nobody open email for the following files \n\n";
    my $hour = (localtime(time))[2];
    
    if ( $hour != 7 && $hour != 17)
    {
        return;
    }
        
    while ( my($key,$value) = each %file_record )
    {
        if ( $DEBUG == 1 )
        {
            my $count = 0;
            foreach my $tmp (keys %$value)  #this part is for testing
            {
                print $count,": ";
                print $value->{$tmp},"\n";
                $count++;
            }
        
            # no notification emails found, compare dates
            $count = scalar(keys %$value);
            print "count: $count\n\n";
        }
        
        if ( scalar(keys %$value) < 8 ) 
        {
            my $report      = $value->{Filename};
            my $recipients  = $value->{Recipient};
            my $sentime     = $value->{Sent_Time};
            my $currenttime =  time(); 
            
            if ($report !~ /No Report/ and 
                $recipients !~ /Nobody/i and 
                ($currenttime - $sentime) / 3600  > 72 )
            {
                $report_list = $report_list."$report\n";
                if ( $key =~ /\d+_\d+_(\d)_\d+/ )
                {
                    if ( $1 eq '1' )
                    {
                        sendmails($alertemail,
                        "A Medlytix Payorintel Report ready for download ");
                    }
                }
            }
        }
    }
    #send_emails($alertemail,$report_list);
}

#------------------------------------------------------------
# void make_subtable($cid, $description)
#       make a subtable in report html
#       Arg: 
#       $client  CustID
#       $description
#
#       No value returned
# -----------------------------------------------------------
sub make_subtable($$)
{
    my $cid = shift;
    my $des = shift;
    

    
    my $filename = "$REPORT.tmp";
    open(FH,">>$filename");
    
    print FH  "<hr color=\"999999\">\n";
    print FH    "<br><br>";
    print FH  "<h4><a name=\"S$cid\">$des</a></h4>\n";
    print FH    "<a href=\"#top1\">TOP</a>";
    print FH  "<table class=\"stats\" cellspacing=\"0\">\n";
    #print FH  "<table  border=\"1\"  cellspacing=\"1\" cellpadding=\"4\">\n";      
    print FH  "\n";
    
    print FH  "<tr><th class=\"hed\">CustID</th>";
    print FH  "<th class=\"hed\">OID</th>";
    print FH  "<th class=\"hed\">File Name</th>";
    print FH  "<th class=\"hed\">Description</th>";
    print FH  "<th class=\"hed\">Recipients</th>";
    print FH  "<th class=\"hed\">Sent Time</th>";
    print FH  "<th class=\"hed\">Opened By</th>";
    print FH  "<th class=\"hed\">Opened Time</th></tr>";
    
    
    my $row_number = 0;
    foreach my $key (sort { $file_record{$a}->{Sent_Time} <=> 
         $file_record{$b}->{Sent_Time} } keys %file_record )
    {
        if ( $file_record{$key}->{Cid} eq $cid )
        {
            if ( scalar(keys %{$file_record{$key}} ) == 8 ) 
            {
                my ( $cc, $oo, $fn, $dp, $reci, $se, $pi, $ot) =
                     (  $file_record{$key}->{Cid},
                        $file_record{$key}->{Oid},
                        $file_record{$key}->{Filename},
                        $file_record{$key}->{Description},
                        $file_record{$key}->{Recipient},
                        $file_record{$key}->{Sent_Time},
                        $file_record{$key}->{OpenBy},
                        $file_record{$key}->{PickupTime}            
                     );
                $reci =~ s/\,/<br>/g;
                
                
                if( $row_number % 2 == 0)
                {
                    print FH  "<tr class=\"normalrow\">";
                }
                else
                {
                    print FH  "<tr class=\"alternaterow\">";
                }
                $row_number++;
                print FH "<td>$cc</td>";
                print FH "<td>$oo</td>";
                print FH "<td>$fn</td>";
                print FH "<td>$dp</td>";
                print FH "<td>$reci</td>";
                print FH "<td>";
                print FH get_short_style_4time($se),"</td>";
                print FH "<td>$pi</td>";
                print FH "<td>";
                print FH get_short_style_4time($ot),"</td>";
        }else
        {
            my ( $cc, $oo, $fn, $dp, $reci, $se ) =
                 (  $file_record{$key}->{Cid},
                    $file_record{$key}->{Oid},
                    $file_record{$key}->{Filename},
                    $file_record{$key}->{Description},
                    $file_record{$key}->{Recipient},
                    $file_record{$key}->{Sent_Time}     
                    );  
                    
            $reci =~ s/\,/<br>/g;   
            my $current = time();
            
            # three days checking
            if ( ($current - $se) / 3600  > 72 &&
                $reci !~ /Nobody/ && 
                !exists $screen_payerintel_files_list{$fn}&&
                is_received_by_another_email($fn) == 0 &&
                $fn !~ /No Report/)
            {
                print FH "<tr class=\"alert\">";
            }else
            {
                if( $row_number % 2 == 0)
                {
                    print FH  "<tr class=\"normalrow\">";
                }
                else
                {
                    print FH  "<tr class=\"alternaterow\">";
                }
                $row_number++;
            }
            print FH "<td>$cc</td>";
            print FH "<td>$oo</td>";
            print FH "<td>$fn</td>";
            print FH "<td>$dp</td>";
            if ( $reci =~ /Nobody/ )
            {
                print FH "<td><font color=\"990000\">$reci</font></td>";
            }else
            {
                print FH "<td>$reci</td>";
            }
            print FH "<td>";
            print FH get_short_style_4time($se),"</td>";
            print FH "<td>\"\"</td>";
            print FH "<td>\"\"</td>";   
        }
            
        } 
    
    }
    print FH "</tr>\n";
    print FH "</table>\n";
    print FH "<br><br>\n";
    close(FH);
}


#------------------------------------------------------------
# void make_subtable_score($cid, $description)
#       make a subtable in scorereport html
#       Arg: 
#       $client  CustID
#       $description
#
#       No value returned
# -----------------------------------------------------------
sub make_subtable_score($$)
{
    my $cid = shift;
    my $des = shift;
    

    
    my $filename = "$score_REPORT.tmp";
    open(FH,">>$filename");
    
    print FH  "<hr color=\"999999\">\n";
    print FH    "<br><br>";
    print FH  "<h4><a name=\"S$cid\">$des</a></h4>\n";
    print FH    "<a href=\"#top1\">TOP</a>";
    print FH  "<table class=\"stats\" cellspacing=\"0\">\n";
    print FH  "\n";
    
    print FH  "<tr><th class=\"hed\">CustID</th>";
    print FH  "<th class=\"hed\">OID</th>";
    print FH  "<th class=\"hed\">File Name</th>";
    print FH  "<th class=\"hed\">Description</th>";
    print FH  "<th class=\"hed\">Recipients</th>";
    print FH  "<th class=\"hed\">Sent Time</th>";
    print FH  "<th class=\"hed\">Opened By</th>";
    print FH  "<th class=\"hed\">Opened Time</th></tr>";
    
    
    my $row_number = 0;
    foreach my $key (sort { $file_record_score{$a}->{Sent_Time} <=> 
         $file_record_score{$b}->{Sent_Time} } keys %file_record_score )
    {
        if ( $file_record_score{$key}->{Cid} eq $cid )
        {
            if ( scalar(keys %{$file_record_score{$key}} ) == 8 ) 
            {
                my ( $cc, $oo, $fn, $dp, $reci, $se, $pi, $ot) =
                     (  $file_record_score{$key}->{Cid},
                        $file_record_score{$key}->{Oid},
                        $file_record_score{$key}->{Filename},
                        $file_record_score{$key}->{Description},
                        $file_record_score{$key}->{Recipient},
                        $file_record_score{$key}->{Sent_Time},
                        $file_record_score{$key}->{OpenBy},
                        $file_record_score{$key}->{PickupTime}          
                     );
                $reci =~ s/\,/<br>/g;
                
                if( $row_number % 2 == 0)
                {
                    print FH  "<tr class=\"normalrow\">";
                }
                else
                {
                    print FH  "<tr class=\"alternaterow\">";
                }
                $row_number++;
                print FH "<td>$cc</td>";
                print FH "<td>$oo</td>";
                print FH "<td>$fn</td>";
                print FH "<td>$dp</td>";
                print FH "<td>$reci</td>";
                print FH "<td>";
                print FH get_short_style_4time($se),"</td>";
                print FH "<td>$pi</td>";
                print FH "<td>";
                print FH get_short_style_4time($ot),"</td>";
        }else
        {
            my ( $cc, $oo, $fn, $dp, $reci, $se ) =
                 (  $file_record_score{$key}->{Cid},
                    $file_record_score{$key}->{Oid},
                    $file_record_score{$key}->{Filename},
                    $file_record_score{$key}->{Description},
                    $file_record_score{$key}->{Recipient},
                    $file_record_score{$key}->{Sent_Time}       
                    );          
            $reci =~ s/\,/<br>/g;   
            my $current = time();
            
            # three days checking
            if ( ($current - $se) / 3600  > 72 &&
                $reci !~ /Nobody/ && 
                !exists $screen_score_files_list{$fn}&&
                $fn !~ /No Report/)
            {
                print FH "<tr class=\"alert\">";
            }else
            {
                if( $row_number % 2 == 0)
                {
                    print FH  "<tr class=\"normalrow\">";
                }
                else
                {
                    print FH  "<tr class=\"alternaterow\">";
                }
                $row_number++;
            }
            print FH "<td>$cc</td>";
            print FH "<td>$oo</td>";
            print FH "<td>$fn</td>";
            print FH "<td>$dp</td>";
            if ( $reci =~ /Nobody/ )
            {
                print FH "<td><font color=\"990000\">$reci</font></td>";
            }else
            {
                print FH "<td>$reci</td>";
            }
            print FH "<td>";
            print FH get_short_style_4time($se),"</td>";
            print FH "<td>\"\"</td>";
            print FH "<td>\"\"</td>";   
        }
            
        } 
    
    }
    print FH "</tr>\n";
    print FH "</table>\n";
    print FH "<br><br>\n";
    close(FH);
}


#------------------------------------------------------------
# void make_uppicked_files_table($cid, $description)
#       make a subtable in report html
#       Arg: 
#       $client  CustID
#       $description
#
#       No value returned
# -----------------------------------------------------------
sub make_uppicked_files_table($$)
{
    my $cid = shift;
    my $des = shift;
    $des =~ s/\s*$//g;
    $des = $des."( Files not picked up )";
    $des =~ s#\(# \(#g;
    my $counter = 0;

    
    my $filename = "$REPORT.tmp";
    open(FH,">>$filename");
    
    
    
    foreach my $key (sort { $file_record{$a}->{Sent_Time} <=> 
         $file_record{$b}->{Sent_Time} } keys %file_record )
    {
        if ( $file_record{$key}->{Cid} eq $cid )
        {
            if ( scalar(keys %{$file_record{$key}} ) < 8 ) 
            {
                my ( $cc, $oo, $fn, $dp, $reci, $se ) =
                 (  $file_record{$key}->{Cid},
                    $file_record{$key}->{Oid},
                    $file_record{$key}->{Filename},
                    $file_record{$key}->{Description},
                    $file_record{$key}->{Recipient},
                    $file_record{$key}->{Sent_Time}     
                    );          
                $reci =~ s/\,/<br>/g;   
                my $current = time();
            
                # three days checking
                if ( ($current - $se) / 3600  > 72 &&
                    $reci !~ /Nobody/ && 
                    is_received_by_another_email($fn) == 0 &&
                    !exists $screen_payerintel_files_list{$fn}&&
                    $fn !~ /No Report/)
                {
                    #   check if cust_id is a ftp client
                    my @is_ftp_custs;
                    undef @is_ftp_custs;
                    for (@ftp_cust_ids) { $is_ftp_custs[$_] = 1; }
                                            
                    if($counter == 0)
                    {
                        print FH  "<hr color=\"999999\">\n";
                        print FH    "<br><br>";
                        print FH  "<h4><a name=\"S${cid}U\">$des</a></h4>\n";
                        print FH    "<a href=\"#top1\">TOP</a>";
                        #print FH  "<table align=\"left\" style=\"width: 100%;\">\n";
                        print FH  "<table  class=\"stats\"  cellspacing=\"0\">\n";      
                        print FH  "\n";
                        print FH  "<tr><th class=\"hed\">CustID</th>";
                        print FH  "<th class=\"hed\">OID</th>";
                        print FH  "<th class=\"hed\">File Name</th>";
                        print FH  "<th class=\"hed\">Description</th>";
                        print FH  "<th class=\"hed\">Recipients</th>";
                        print FH  "<th class=\"hed\">Sent Time</th>";
                        print FH  "<th class=\"hed\">Opened By</th>";
                        print FH  "<th class=\"hed\">Opened Time</th>";
                        if($is_ftp_custs[$cc])
                        {
                            print FH "</tr>\n";
                        }
                        else
                        {
                            print FH  "<th class=\"hed\"></th></tr>\n"
                        }
                    }
                    $counter++;
                    print FH "<tr class=\"alert\">";
        
                    print FH "<td>$cc</td>";
                    print FH "<td>$oo</td>";
                    print FH "<td>$fn</td>";
                    print FH "<td>$dp</td>";
                    print FH "<td>$reci</td>";
                    print FH "<td>";
                    print FH get_short_style_4time($se),"</td>";
                    print FH "<td>\"\"</td>";
                    print FH "<td>\"\"</td>";   
                    
                    #   screen ftp clients
                    if($is_ftp_custs[$cc])
                    {
                        
                    }
                    else
                    {
                        print FH "<td><a href=\"overwrite.php?filename=$fn&next=$REPORT_File_Name&place=S${cid}U\">";
                        print FH "overwrite</a></td>\n";
                    }
                }
            }
            
        } 
    
    }
    
    if($counter > 0)
    {
        print FH "</tr>";
        print FH "</table>\n";
        print FH "<br><br>";
    }
    close(FH);
}


#------------------------------------------------------------
# void make_uppicked_files_table_separate($cid, $description)
#       make a subtable in report html in separate format
#            especially for cliens having lots of files
#       Arg: 
#       $client  CustID
#       $description
#
#       No value returned
# -----------------------------------------------------------
sub make_uppicked_files_table_separate($$)
{
    my $cid = shift;
    my $des = shift;
    $des =~ s/\s*$//g;
    $des = $des."( Files not picked up";
    $des =~ s#\(# \(#g;
    my %counter_for_date = ();
    my $current_date;

    
    my $filename = "$REPORT.tmp";
    open(FH,">>$filename");
    
    
    
    foreach my $key (sort { $file_record{$a}->{Sent_Time} <=> 
         $file_record{$b}->{Sent_Time} } keys %file_record )
    {
        if ( $file_record{$key}->{Cid} eq $cid )
        {
            if ( scalar(keys %{$file_record{$key}} ) < 8 ) 
            {
                my ( $cc, $oo, $fn, $dp, $reci, $se ) =
                 (  $file_record{$key}->{Cid},
                    $file_record{$key}->{Oid},
                    $file_record{$key}->{Filename},
                    $file_record{$key}->{Description},
                    $file_record{$key}->{Recipient},
                    $file_record{$key}->{Sent_Time}     
                    );          
                $reci =~ s/\,/<br>/g;   
                my $current = time();
            
                # three days checking
                if ( ($current - $se) / 3600  > 72 &&
                    $reci !~ /Nobody/ && 
                    is_received_by_another_email($fn) == 0 &&
                    !exists $screen_payerintel_files_list{$fn}&&
                    $fn !~ /No Report/)
                {
                    #   check if cust_id is a ftp client
                    my @is_ftp_custs;
                    undef @is_ftp_custs;
                    for (@ftp_cust_ids) 
                    { 
                        $is_ftp_custs[$_] = 1; 
                    }
                    
                    if (!exists($counter_for_date{get_date($se)}))
                    {
                        $counter_for_date{get_date($se)} = 1;
                        if(defined $current_date)
                        {
                            print FH "</tr>";
                            print FH "</table>\n";
                            print FH "<br><br>";
                        }
                        $current_date = get_date($se);
                    }
                    else
                    {
                        $counter_for_date{$current_date}++;
                    }
                    
                    if($counter_for_date{$current_date} == 1)
                    {
                        print FH  "<hr color=\"999999\">\n";
                        print FH    "<br><br>";
                        print FH  "<h4><a name=\"S${cid}U";
                        print FH  get_date($se);
                        print FH  "\">$des sent on $current_date )</a></h4>\n";
                        print FH    "<a href=\"#top1\">TOP</a>";
                        #print FH  "<table align=\"left\" style=\"width: 100%;\">\n";
                        print FH  "<table  class=\"stats\"  cellspacing=\"0\">\n";      
                        print FH  "\n";
                        print FH  "<tr><th class=\"hed\">CustID</th>";
                        print FH  "<th class=\"hed\">OID</th>";
                        print FH  "<th class=\"hed\">File Name</th>";
                        print FH  "<th class=\"hed\">Description</th>";
                        print FH  "<th class=\"hed\">Recipients</th>";
                        print FH  "<th class=\"hed\">Sent Time</th>";
                        print FH  "<th class=\"hed\">Opened By</th>";
                        print FH  "<th class=\"hed\">Opened Time</th>";
                        
                        #   add one more cell for non-ftp clients
                        if($is_ftp_custs[$cc])
                        {
                            print FH "</tr>\n";
                        }
                        else
                        {
                            print FH  "<th class=\"hed\"></th></tr>\n"
                        }
                    }
                    print FH "<tr class=\"alert\">";
        
                    print FH "<td>$cc</td>";
                    print FH "<td>$oo</td>";
                    print FH "<td>$fn</td>";
                    print FH "<td>$dp</td>";
                    print FH "<td>$reci</td>";
                    print FH "<td>";
                    print FH get_short_style_4time($se),"</td>";
                    print FH "<td>\"\"</td>";
                    print FH "<td>\"\"</td>";   
                    
                    #   add one more cell but screen ftp clients
                    if($is_ftp_custs[$cc])
                    {
                        
                    }
                    else
                    {
                        print FH "<td><a href=\"overwrite.php?filename=$fn&next=$REPORT_File_Name&place=S${cid}U";
                        print FH get_date($se);
                        print FH "\">";
                        print FH "overwrite</a></td>\n";
                    }
                }
            }
            
        } 
    
    }
    
    if(defined $current_date)
    {
        print FH "</tr>";
        print FH "</table>\n";
        print FH "<br><br>";
    }
    close(FH);
}

#------------------------------------------------------------
# void make_unpicked_index_score($cid, *filehandle)
#       make the index for unpicked files on the top of page
#       Arg: 
#       $client  CustID
#       *filehandle
#
#       No value returned
# -----------------------------------------------------------
sub make_unpicked_index_score($$)
{
    my $cid = shift;
    local *FH = shift;
    my $self_count = 10;
    my $previous_date = ();
    
    foreach my $key (sort { $file_record_score{$b}->{Sent_Time} <=> 
         $file_record_score{$a}->{Sent_Time} } keys %file_record_score )
    {
        if ( $file_record_score{$key}->{Cid} eq $cid )
        {
            if ( scalar(keys %{$file_record_score{$key}} ) < 8 && 
                    $self_count > 0) 
            {
                
                my ( $cc, $oo, $fn, $reci, $se ) =
                 (  $file_record_score{$key}->{Cid},
                    $file_record_score{$key}->{Oid},
                    $file_record_score{$key}->{Filename},
                    $file_record_score{$key}->{Recipient},
                    $file_record_score{$key}->{Sent_Time}       
                    );          
                $reci =~ s/\,/<br>/g;   
                my $current = time();
            
                # check if file has been picked up
                if ( ($current - $se) / 3600  > 0 &&
                    $reci !~ /Nobody/ && 
                    !exists $screen_score_files_list{$fn}&&
                    $fn !~ /No Report/ &&
                    is_same_date($previous_date,$se) == 0 )
                {
                        $self_count--;
                        print FH "<td bgcolor=\"66FF66\">";
                        print FH "<a href=\"#S$cid";
                        print FH "\">";
                        print FH get_date($se);
                        print FH "</a>";
                        print FH "</td>";
                        print FH "\n";
                        $previous_date = $se;   
                }
            }
        } 
    }
    
    for(;$self_count > 0; $self_count--)
    {
        print FH "<td>&nbsp</td>\n"
    }
}


#------------------------------------------------------------
# void make_unpicked_index($cid, *filehandle)
#       make the index for unpicked files on the top of page
#       Arg: 
#       $client  CustID
#       *filehandle
#
#       No value returned
# -----------------------------------------------------------
sub make_unpicked_index($$)
{
    my $cid = shift;
    local *FH = shift;
    my $self_count = 10;
    my $previous_date = ();
    
    foreach my $key (sort { $file_record{$b}->{Sent_Time} <=> 
         $file_record{$a}->{Sent_Time} } keys %file_record )
    {
        if ( $file_record{$key}->{Cid} eq $cid )
        {
            if ( scalar(keys %{$file_record{$key}} ) < 8 && 
                    $self_count > 0) 
            {
                
                my ( $cc, $oo, $fn, $reci, $se ) =
                 (  $file_record{$key}->{Cid},
                    $file_record{$key}->{Oid},
                    $file_record{$key}->{Filename},
                    $file_record{$key}->{Recipient},
                    $file_record{$key}->{Sent_Time}     
                    );          
                $reci =~ s/\,/<br>/g;   
                my $current = time();
            
                # three days checking
                if ( ($current - $se) / 3600  > 72 &&
                    $reci !~ /Nobody/ && 
                    !exists $screen_payerintel_files_list{$fn}&&
                    is_received_by_another_email($fn) == 0 &&
                    $fn !~ /No Report/ && 
                        is_same_date($previous_date,$se) == 0)
                {
                    if($cc == 49)
                    {
                        $self_count--;
                        print FH "<td bgcolor=\"66FF66\">";
                        print FH "<a href=\"#S";
                        print FH $cid;
                        print FH "U";
                        print FH get_date($se);
                        print FH "\">";
                        print FH get_date($se);
                        print FH "</a>";
                        print FH "</td>";
                        print FH "\n";
                        $previous_date = $se;
                    }
                    else
                    {
                        $self_count--;
                        print FH "<td bgcolor=\"66FF66\">";
                        print FH "<a href=\"#S";
                        print FH $cid;
                        print FH "U";
                        print FH "\">";
                        print FH get_date($se);
                        print FH "</a>";
                        print FH "</td>";
                        print FH "\n";
                        $previous_date = $se;
                    }   
                }
                
            }
        } 
    }
    
    for(;$self_count > 0; $self_count--)
    {
        print FH "<td>&nbsp</td>\n"
    }
}

#------------------------------------------------------------
# void make_report(void)
#       make a text file to report who opent the emails
#        
#
#       No value returned
# -----------------------------------------------------------
sub make_report()
{
    
    my @clientlists = ();
    my %seen = ();
    
    
    
    #   fetch clients' name 
    foreach my $key (keys %file_record )
    {
        if( defined($seen{$file_record{$key}->{Cid}}) )
        {
            next;
        }
        push @clientlists, $file_record{$key}->{Cid};
        if ( exists($org_hash{ $file_record{$key}->{Cid}} ) )
        {
            $seen{$file_record{$key}->{Cid}} = $org_hash{$file_record{$key}->{Cid}};
        }else
        {
            $seen{$file_record{$key}->{Cid}} = $file_record{$key}->{Description};
        }    
    }
    
    my $mmddyyyy = strftime("%m/%d/%Y %H:%M:%S", localtime);
    my $filename = "$REPORT.tmp";
    open(FH,">$filename");
    
    print FH  "<html  style=\"height: 100%;\">\n";
    print FH  "<head>\n";
    print FH  "<title>File Pickup Report</title>\n";
    print FH  "<link rel=\"stylesheet\" type=\"text/css\" href=\"./includes/etrack.css\"/>\n";
    print FH  "</head>\n";
    print FH  "<body TOPMARGIN=\"10\" MARGINHEIGHT=\"50\"  style=\"height: 100%;\">\n";
    
    #   write medlytix icon
    print FH "<div align=\"left\">";
    print FH "<img src=\"./includes/menu_r1_c1.gif\"></div>\n";
    print FH "<br><br>\n";
    
    #   write headline
    print FH  "<h2><b><a name=\"top1\"><font size=\"6\">File Pickup Report</font></a></b></h2>";
    print FH  "<p>Generated at: ";
    print FH  $mmddyyyy,"</p>\n";
    #print FH  "<hr color=\"999999\" width=\"100%\">\n";
    
    
    
    
    #   write tabs
    print FH  "<br><br><ul id=\"tablist\">\n";
    print FH  "<li><a class=\"current\" href=\"report.html\">";
    print FH  "payerintel</a></li>\n";
    print FH  "<li><a href=\"scoreport.html\">score</a></li>\n";
    print FH  "</ul><br>\n";
    
    
    #   write the dashboad table
    print FH    "<br>\n";
    #print FH  "<table align=\"left\" style=\"width: 100%;\">\n";
    print FH    "<table class=\"stats\" cellspacing=\"0\">\n";
    #print FH  "<table border=\"1\"  cellspacing=\"1\" cellpadding=\"4\">\n";
    print FH  "<tr><th class=\"longhed\">Client Name</th>\n";
    print FH  "<th class=\"hed\" colspan=\"10\">Files Not Picked Up</th></tr>\n";
    print FH    "<tr class=\"normalrow\"><td class=\"content\">";
    
    print FH  "<p><a href=\"#C4\">All Clients</a></p></td>";
    for(my $count=0; $count < 10; $count++ )
    {
        print FH "<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td>";
    }
    print FH  "</tr>\n";
    
    my $row_number = 0;
    foreach my $client (sort { lc($seen{ $a }) cmp lc($seen{ $b })} @clientlists)
    {
        if( $row_number % 2 == 0)
        {
            print FH  "<tr class=\"alternaterow\">";
        }
        else
        {
            print FH  "<tr class=\"normalrow\">";
        }
        $row_number++;
        print FH  "<td class=\"content\">";
        print FH  "<p><a href=\"#S$client\">$seen{ $client }</a></p>";
        print FH  "</td>\n";
        make_unpicked_index($client, *FH);
        print FH  "</tr>\n";
    }
    print FH "</table>\n";
     
    
    print FH  "<br>\n";
    print FH  "<hr color=\"999999\">\n";
    print FH    "<br>\n";
    
    
    #   write main table
    print FH  "<h4><a name=\"C4\">All Clients</a></h4>";
    print FH    "<a href=\"#top1\">TOP</a>";
    print FH  "<table class=\"stats\" cellspacing=\"0\">\n";
    #print FH  "<table border=\"1\"  cellspacing=\"1\" cellpadding=\"4\">\n";
    print FH  "\n";
    print FH  "<tr><th class=\"hed\">CustID</th>";
    print FH  "<th class=\"hed\">OID</th>";
    print FH  "<th class=\"hed\">File Name</th>";
    print FH  "<th class=\"hed\">Description</th>";
    print FH  "<th class=\"hed\">Recipients</th>";
    print FH  "<th class=\"hed\">Sent Time</th>";
    print FH  "<th class=\"hed\">Opened By</th>";
    print FH  "<th class=\"hed\">Opened Time</th></tr>\n";
    
    $row_number = 0;
    foreach my $key (sort { $file_record{$a}->{Sent_Time} <=> 
         $file_record{$b}->{Sent_Time} } keys %file_record )
    {
        if ( scalar(keys %{$file_record{$key}} ) == 8 ) 
        {
            my ( $cc, $oo, $fn, $dp, $reci, $se, $pi, $ot) =
                 (  $file_record{$key}->{Cid},
                    $file_record{$key}->{Oid},
                    $file_record{$key}->{Filename},
                    $file_record{$key}->{Description},
                    $file_record{$key}->{Recipient},
                    $file_record{$key}->{Sent_Time},
                    $file_record{$key}->{OpenBy},
                    $file_record{$key}->{PickupTime}    
                    );
            #print "cc=$cc, oo=$oo\n rece=$reci, se=$se, pi = $pi, ot=$ot\n-------------------**\n";
             
            $reci =~ s/\,/<br>/g;
            if( $row_number % 2 == 0)
            {
                print FH  "<tr class=\"normalrow\">";
            }
            else
            {
                print FH  "<tr class=\"alternaterow\">";
            }
            $row_number++;
            
            print FH "<td>$cc</td>";
            print FH "<td>$oo</td>";
            if(!$fn)
            {
                print $key;
            }
            
            print FH "<td>$fn</td>";
            print FH "<td>$dp</td>";
            print FH "<td>$reci</td>";
            print FH "<td>";
            print FH get_short_style_4time($se),"</td>";
            print FH "<td>$pi</td>";
            print FH "<td>";
            print FH get_short_style_4time($ot),"</td>";
        }else
        {
            my ( $cc, $oo, $fn, $dp, $reci, $se ) =
                 (  $file_record{$key}->{Cid},
                    $file_record{$key}->{Oid},
                    $file_record{$key}->{Filename},
                    $file_record{$key}->{Description},
                    $file_record{$key}->{Recipient},
                    $file_record{$key}->{Sent_Time}     
                    );
            $reci =~ s/\,/<br>/g;   
            my $current = time();
            
            # three days checking
            if ( ($current - $se) / 3600  > 72 &&
                 $reci !~ /Nobody/ &&
                 is_received_by_another_email($fn) == 0 &&
                !exists $screen_payerintel_files_list{$fn}&&
                 $fn !~ /No Report/ ) 
            {
                print FH "<tr class=\"alert\">";
                
                #   to record files not picked up currently
                $payerintel_files_not_picked_up{$fn} = $cc;
                
            }else
            {
                if ( is_received_by_another_email($fn) == 0 &&
                        !exists $screen_payerintel_files_list{$fn} &&
                        $fn !~ /No Report/ )
                {
                    #   to record files not picked up currently
                    $payerintel_files_not_picked_up{$fn} = $cc;     
                }
                
                if( $row_number % 2 == 0)
                {
                    print FH  "<tr class=\"normalrow\">";
                }
                else
                {
                    print FH  "<tr class=\"alternaterow\">";
                }
                $row_number++;
            }
            print FH "<td>$cc</td>";
            print FH "<td>$oo</td>";
            print FH "<td>$fn</td>";
            print FH "<td>$dp</td>";
            if ( $reci =~ /Nobody/ )
            {
                print FH "<td><font color=\"990000\">$reci</font></td>";
            }else
            {
                print FH "<td>$reci</td>";
            }
            print FH "<td>";
            print FH get_short_style_4time($se),"</td>";
            print FH "<td>\"\"</td>";
            print FH "<td>\"\"</td>";   
        }
     
        print FH "</tr>\n";
    }
    print FH "</table>\n";
    print FH "<br><br>\n";
    close(FH);
    
    #   make subtable for every client
    foreach my $custid (@clientlists)
    
    {
        make_subtable($custid, $seen{$custid});
    }
    
    #   make unpicked up files subtable for every client
    foreach my $custid (@clientlists)
    
    {
        if($custid == 49)
        {
            make_uppicked_files_table_separate($custid, $seen{$custid});
        }
        else
        {
            make_uppicked_files_table($custid, $seen{$custid});
        }
        
    }
    
    open(FH,">>$filename");
    print FH  "<br><br><br>\n";
    print FH  "<hr color=\"999999\" width=\"100%\" ";
    print FH  "<p align=\"right\">Version: $software_version</p>\n";
    print FH  "<br><br><br><br><br>\n";
    print FH "</body>\n";
    print FH "</html>\n";
    close(FH);
    
    # remove the previous one if it exists
    if (-e $REPORT)
    {
         unlink($REPORT);
    }
    
    my $cmd = "mv $filename $REPORT";
    while( system($cmd) )
    {
        sleep 10;
    }
}

#------------------------------------------------------------
# void make_report_score(void)
#       make a text file to report who opent the emails
#        
#
#       No value returned
# -----------------------------------------------------------
sub make_report_score()
{
    
    my @clientlists = ();
    my %seen = ();
    
    
    #   fetch clients' name 
    foreach my $key (keys %file_record_score )
    {
        if( defined($seen{$file_record_score{$key}->{Cid}}) )
        {
            next;
        }
        push @clientlists, $file_record_score{$key}->{Cid};
        if ( exists($org_hash{ $file_record_score{$key}->{Cid}} ) )
        {
            $seen{$file_record_score{$key}->{Cid}} = $org_hash{$file_record_score{$key}->{Cid}};
        }else
        {
            $seen{$file_record_score{$key}->{Cid}} = $file_record_score{$key}->{Description};
        }    
    }
    
    my $mmddyyyy = strftime("%m/%d/%Y %H:%M:%S", localtime);
    my $filename = "$score_REPORT.tmp";
    open(FH,">$filename");
    
    print FH  "<html  style=\"height: 100%;\">\n";
    print FH  "<head>\n";
    print FH  "<title>File Pickup Report</title>\n";
    print FH  "<link rel=\"stylesheet\" type=\"text/css\" href=\"./includes/etrack.css\"/>\n";
    print FH  "</head>\n";
    print FH  "<body TOPMARGIN=\"10\" MARGINHEIGHT=\"50\"  style=\"height: 100%;\">\n";
    
    #   write medlytix icon
    print FH "<div align=\"left\">";
    print FH "<img src=\"./includes/menu_r1_c1.gif\"></div>\n";
    print FH "<br><br>\n";
    
    #   write headline
    print FH  "<h2><b><a name=\"top1\"><font size=\"6\">File Pickup Report</font></a></b></h2>";
    print FH  "<p>Generated at: ";
    print FH  $mmddyyyy,"</p>\n";
    #print FH  "<hr color=\"999999\" width=\"100%\">\n";
    
    
    
    
    #   write tabs
    print FH  "<br><br><ul id=\"tablist\">\n";
    print FH  "<li><a  href=\"report.html\">";
    print FH  "payerintel</a></li>\n";
    print FH  "<li><a class=\"current\" href=\"scoreport.html\">score</a></li>\n";
    print FH  "</ul><br>\n";
    
    
    #   write the dashboad table
    print FH    "<br>\n";
    #print FH  "<table align=\"left\" style=\"width: 100%;\">\n";
    print FH    "<table class=\"stats\" cellspacing=\"0\">\n";
    #print FH  "<table border=\"1\"  cellspacing=\"1\" cellpadding=\"4\">\n";
    print FH  "<tr><th class=\"longhed\">Client Name</th>\n";
    print FH  "<th class=\"hed\" colspan=\"10\">Files Not Picked Up</th></tr>\n";
    print FH    "<tr class=\"normalrow\"><td class=\"content\">";
    
    print FH  "<p><a href=\"#C4\">All Clients</a></p></td>";
    for(my $count=0; $count < 10; $count++ )
    {
        print FH "<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td>";
    }
    print FH  "</tr>\n";
    
    my $row_number = 0;
    foreach my $client (sort { lc($seen{ $a }) cmp lc($seen{ $b })} @clientlists)
    {
        if( $row_number % 2 == 0)
        {
            print FH  "<tr class=\"alternaterow\">";
        }
        else
        {
            print FH  "<tr class=\"normalrow\">";
        }
        $row_number++;
        print FH  "<td class=\"content\">";
        print FH  "<p><a href=\"#S$client\">$seen{ $client }</a></p>";
        print FH  "</td>\n";
        make_unpicked_index_score($client, *FH);
        print FH  "</tr>\n";
    }
    print FH "</table>\n";
     
    
    print FH  "<br>\n";
    print FH  "<hr color=\"999999\">\n";
    print FH    "<br>\n";
    
    
    #   write main table
    print FH  "<h4><a name=\"C4\">All Clients</a></h4>";
    print FH    "<a href=\"#top1\">TOP</a>";
    print FH  "<table class=\"stats\" cellspacing=\"0\">\n";
    #print FH  "<table border=\"1\"  cellspacing=\"1\" cellpadding=\"4\">\n";
    print FH  "\n";
    print FH  "<tr><th class=\"hed\">CustID</th>";
    print FH  "<th class=\"hed\">OID</th>";
    print FH  "<th class=\"hed\">File Name</th>";
    print FH  "<th class=\"hed\">Description</th>";
    print FH  "<th class=\"hed\">Recipients</th>";
    print FH  "<th class=\"hed\">Sent Time</th>";
    print FH  "<th class=\"hed\">Opened By</th>";
    print FH  "<th class=\"hed\">Opened Time</th></tr>\n";
    
    $row_number = 0;
    
    foreach my $key (sort { $file_record_score{$a}->{Sent_Time} <=> 
         $file_record_score{$b}->{Sent_Time} } keys %file_record_score )
    {
         
        if ( scalar(keys %{$file_record_score{$key}} ) == 8 ) 
        {
            my ( $cc, $oo, $fn, $dp, $reci, $se, $pi, $ot) =
                 (  $file_record_score{$key}->{Cid},
                    $file_record_score{$key}->{Oid},
                    $file_record_score{$key}->{Filename},
                    $file_record_score{$key}->{Description},
                    $file_record_score{$key}->{Recipient},
                    $file_record_score{$key}->{Sent_Time},
                    $file_record_score{$key}->{OpenBy},
                    $file_record_score{$key}->{PickupTime}  
                    );
            #print "cc=$cc, oo=$oo\n rece=$reci, se=$se, pi = $pi, ot=$ot\n-------------------**\n";
            $reci =~ s/\,/<br>/g;
            if( $row_number % 2 == 0)
            {
                print FH  "<tr class=\"normalrow\">";
            }
            else
            {
                print FH  "<tr class=\"alternaterow\">";
            }
            $row_number++;
            
            print FH "<td>$cc</td>";
            print FH "<td>$oo</td>";
            print FH "<td>$fn</td>";
            print FH "<td>$dp</td>";
            print FH "<td>$reci</td>";
            print FH "<td>";
            print FH get_short_style_4time($se),"</td>";
            print FH "<td>$pi</td>";
            print FH "<td>";
            print FH get_short_style_4time($ot),"</td>";
        }else
        {
            my ( $cc, $oo, $fn, $dp, $reci, $se ) =
                 (  $file_record_score{$key}->{Cid},
                    $file_record_score{$key}->{Oid},
                    $file_record_score{$key}->{Filename},
                    $file_record_score{$key}->{Description},
                    $file_record_score{$key}->{Recipient},
                    $file_record_score{$key}->{Sent_Time}       
                    );
            $reci =~ s/\,/<br>/g;   
            my $current = time();
            
            # three days checking
            if ( ($current - $se) / 3600  > 72 &&
                 $reci !~ /Nobody/ &&
                 is_received_by_another_email($fn) == 0 &&
                !exists $screen_payerintel_files_list{$fn}&&
                 $fn !~ /No Report/ ) 
            {
                print FH "<tr class=\"alert\">";
            }else
            {
                if( $row_number % 2 == 0)
                {
                    print FH  "<tr class=\"normalrow\">";
                }
                else
                {
                    print FH  "<tr class=\"alternaterow\">";
                }
                $row_number++;
            }
            print FH "<td>$cc</td>";
            print FH "<td>$oo</td>";
            print FH "<td>$fn</td>";
            print FH "<td>$dp</td>";
            if ( $reci =~ /Nobody/ )
            {
                print FH "<td><font color=\"990000\">$reci</font></td>";
            }else
            {
                print FH "<td>$reci</td>";
            }
            print FH "<td>";
            print FH get_short_style_4time($se),"</td>";
            print FH "<td>\"\"</td>";
            print FH "<td>\"\"</td>";   
        }
     
        print FH "</tr>\n";
    }
    
    print FH "</table>\n";
    print FH "<br><br>\n";
    close(FH);
    
    #   make subtable for every client
    foreach my $custid (@clientlists)
    
    {
        make_subtable_score($custid, $seen{$custid});
    }
    
    #   make unpicked up files subtable for every client
#   foreach my $custid (@clientlists)
#   
#   {
#       if($custid == 49)
#       {
#           make_uppicked_files_table_separate($custid, $seen{$custid});
#       }
#       else
#       {
#           make_uppicked_files_table($custid, $seen{$custid});
#       }
#       
#   }
    
    open(FH,">>$filename");
    print FH  "<br><br><br>\n";
    print FH  "<hr color=\"999999\" width=\"100%\" ";
    print FH  "<p align=\"right\">Version: $software_version</p>\n";
    print FH  "<br><br><br><br><br>\n";
    print FH "</body>\n";
    print FH "</html>\n";
    close(FH);
    
    # remove the previous one if it exists
    if (-e $score_REPORT)
    {
         unlink($score_REPORT);
    }
    
    
    my $cmd = "mv $filename $score_REPORT";
    while( system($cmd) )
    {
        sleep 10;
    }
}

#------------------------------------------------------
# Daemon must handle the following sigs
#
# -----------------------------------------------------
sub sighandler($)
{
    my $sig_id = shift;
    $to_stop = 1;
}


sub install_sighandler()
{
    my $sigset = POSIX::SigSet->new();
    my $action = POSIX::SigAction->new('sighandler',
                                        $sigset, &POSIX::SA_NODEFER);
    sigaction(&SIGINT,  $action);
    sigaction(&SIGTERM, $action);
    sigaction(&SIGQUIT, $action);
    sigaction(&SIGPIPE, $action);
}

#------------------------------------------------------------
# void run_payerintel(void)
#       run modules for payerintel files
#        
#
#       No value returned
# -----------------------------------------------------------
sub run_payerintel()
{
    if ( $DEBUG == 1 )
    {
        print "..Payerintel Start ...\n";
    }
     
    read_screen_payerintel_files_list;
    check_email_account;
    get_ftp_email;
    send_alert_emails;
    write_backup(\%file_record,"payerintel");
    make_report;
    update_dashboard();
    
    if ( $DEBUG == 1 )
    {
        print "..Payerintel End ...\n";
    }
     
    %file_record = ();
    %screen_payerintel_files_list = ();
    %payerintel_files_not_picked_up = ();
}

#------------------------------------------------------------
# void run_payerintel(void)
#       run modules for payerintel files
#        
#
#       No value returned
# -----------------------------------------------------------
sub run_score()
{
    if ( $DEBUG == 1 )
    {
         print "..Score Start ...\n";
    }
    
    read_screen_score_files_list;
    check_email_account_score;
    get_ftp_email_score;
    
    write_backup(\%file_record_score,"score");
    make_report_score;
    
    if ( $DEBUG == 1 )
    {
         print " ..Score End ...\n";
    }
    
    %file_record_score = ();
    %screen_score_files_list    =   ();
}

#------------------main-----------------------------------
#   start daemon
#
#---------------------------------------------------------
#print " ..running ...\n";
#system("date");
#if ( $DEBUG != 1 )
#{
#   Proc::Daemon::Init();
#   install_sighandler();
#}

chdir($DIR);

#while (!$to_stop)
#{
    $today = get_sent_date();
    #sleep 61;
    #read_cust_name();
      
    run_payerintel;
    run_score;
    exit 0;
#   if ( $DEBUG != 1 )
#   {
#       my $sleeptime = get_sleeptime;
#       sleep $sleeptime;
#   }
#}
